//package com._4pd.demo.formal.watcher;
//
//import com._4pd.demo.demo.crd.AiOperator;
//import com._4pd.demo.demo.crd.AiOperatorStatus;
//import com._4pd.demo.formal.K8sClient;
//import com._4pd.demo.formal.entity.K8sCommonResource;
//import com._4pd.demo.formal.template.AiTemplateRender;
//import com._4pd.demo.formal.template.FreeMakerRender;
//import com._4pd.demo.formal.template.JinJaRender;
//import com.alibaba.fastjson.JSON;
//import freemarker.template.Configuration;
//import freemarker.template.Template;
//import freemarker.template.Version;
//import io.fabric8.kubernetes.api.model.GenericKubernetesResource;
//import io.fabric8.kubernetes.client.KubernetesClient;
//import io.fabric8.kubernetes.client.WatcherException;
//import io.fabric8.kubernetes.client.dsl.Resource;
//import io.fabric8.kubernetes.client.dsl.base.ResourceDefinitionContext;
//import org.yaml.snakeyaml.Yaml;
//
//import java.io.ByteArrayInputStream;
//import java.io.File;
//import java.io.IOException;
//import java.io.StringWriter;
//import java.util.HashMap;
//import java.util.Map;
//
//public class AiOperatorWatcher extends StatusWatcher {
//    private AiTemplateRender aiTemplateRender = new JinJaRender();
//
//    public AiOperatorWatcher(K8sClient client) throws IOException {
//        super(client);
//    }
//
//    @Override
//    void AddedAction(GenericKubernetesResource resource) {
//        KubernetesClient client = this.client.getClient();
//        try {
//            String resourceName = resource.getMetadata().getName();
//            String resourceYaml = aiTemplateRender.render(resource);
//
//            ResourceDefinitionContext rdc = new Yaml().loadAs(resourceYaml, K8sCommonResource.class).buildResourceDefinitionContext();
//            // 创建template，标记 crd name
//            Resource<GenericKubernetesResource> resourceResource = client.genericKubernetesResources(rdc)
//                    .inNamespace("airflow").load(new ByteArrayInputStream(resourceYaml.getBytes()));
//
//            if (client.genericKubernetesResources(rdc).inNamespace("airflow").withName(resourceName).isReady()) {
//                System.out.println("resource: " + " [" + resourceName + "] " + " already exist");
//                return;
//            }
//            System.out.println(resourceYaml);
//            resourceResource.get().getMetadata().getLabels().put("task", String.valueOf(resourceName));
//            resourceResource.create();
//            //标记 crd 信息
//            client.resource(resource).edit(aiOperator -> {
//                Map<String, String> labels = aiOperator.getMetadata().getLabels();
//                labels.put("task", String.valueOf(resourceName));
//                labels.put("sub_task_type", rdc.getKind() + "_" + rdc.getGroup() + "_" + rdc.getVersion());
//                return aiOperator;
//            });
//
////           注册一个子资源状态的监听
//            this.client.registerResourceWatcher(rdc.getVersion(),rdc.getKind());
//
//        } catch (Exception e) {
//            e.printStackTrace();
//            client.resource(resource).edit(aiOperator -> {
//                AiOperatorStatus status = aiOperator.get("status");
//                return aiOperator;
//            });
//        }
//    }
//
//    @Override
//    void ModifiedAction(GenericKubernetesResource resource) {
//        System.out.println("ModifiedAction");
//    }
//
//    @Override
//    void DeletedAction(GenericKubernetesResource resource) {
//        String[] split = resource.getMetadata().getLabels().get("sub_task_type").split("_");
//        Resource<GenericKubernetesResource> resourceResource = client.getClient().genericKubernetesResources(new ResourceDefinitionContext.Builder()
//                .withKind(split[0])
//                .withGroup(split[1])
//                .withVersion(split[2])
//                .withNamespaced(true)
//                .build()).inNamespace("airflow").withName(resource.getMetadata().getLabels().get("task"));
//        resourceResource.delete();
//    }
//
//    @Override
//    void ErrorAction(GenericKubernetesResource resource) {
//        System.out.println("ErrorAction");
//    }
//
//    @Override
//    void BookmarkAction(GenericKubernetesResource resource) {
//        System.out.println("BookmarkAction");
//    }
//
//    @Override
//    public void onClose(WatcherException cause) {
//        cause.printStackTrace();
//    }
//
//}
